package com.stp.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import com.stp.rabbitmq.config.DeadConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Slf4j
@Component
public class DeadRabbitmqListener {

    /**
     * 消费者拒绝接收消息，并拒绝将消息重新放回队列
     * channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
     */

    /**
     * 监听死信队列
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {DeadConfig.DEAD_QUEUE_NAME})
    public void deadListener(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("死信队列接收到的消息内容：" + msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
